package com.golang.api.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.golang.api.common.ActivityStatusType;
import com.golang.api.common.OrderStatusType;
import com.golang.api.domain.*;
import com.golang.api.dto.request.*;
import com.golang.api.dto.response.order.CreateOrderDto;
import com.golang.api.repository.ActivityRepository;
import com.golang.api.repository.OrderRepository;
import com.golang.api.service.IOrderService;
import com.golang.api.util.Result;
import com.golang.api.util.ResultGenerator;
import com.golang.api.util.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 订单Service业务层处理
 */
@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {

    @Resource
    ActivityRepository activityRepository;

    @Resource
    OrderRepository orderRepository;


    @Override
    public Result<CreateOrderDto> create(OrderInfoCreateDto dto) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();
        List<String> picList = objectMapper.readValue(dto.getPicArr(), objectMapper.getTypeFactory().constructCollectionType(List.class, String.class));
        List<OrderTicketCreateDto> ticketList = objectMapper.readValue(dto.getTicketList(), objectMapper.getTypeFactory().constructCollectionType(List.class, OrderTicketCreateDto.class));

        Activity activity = activityRepository.queryActivity(dto.getActivityId());

        //活动状态判断
        if (!Objects.equals(activity.getActivityStatus(), ActivityStatusType.SignUp.getDbType())) {
            throw new ServiceException(500, "活动" + activity.getActivityTitle() + ActivityStatusType.getByDbType(activity.getActivityStatus()).getShowName() + ",已无法报名");
        }

        //购买截止日期判断

        OrderInfo orderInfo = activityBasic(dto).apply(dto);

        if (orderRepository.saveOrderInfo(orderInfo)) {
            //保存图片
            picList.forEach(o -> {
                OrderPic pic = OrderPicDto2OrderPic(o, orderInfo.getOrderId()).apply(o);
                orderRepository.insertOrderPic(pic);
            });

            BigDecimal totalAmount = BigDecimal.ZERO;
            List<OrderItem> itemList = new ArrayList<>();
            //保存订单明细
            for (OrderTicketCreateDto ticketDto : ticketList) {
                ActivityTicket ticket = activityRepository.queryActivityTicket(ticketDto.getTicketId());
                OrderItem item = OrderItemDto2OrderItem(ticketDto, orderInfo.getOrderId(), ticket).apply(ticketDto);
                orderRepository.insertOrderItem(item);
                totalAmount = totalAmount.add(item.getAmount());
                itemList.add(item);
            }

            orderInfo.setTotalAmount(totalAmount);
            orderRepository.saveOrderInfo(orderInfo);

            return ResultGenerator.success(CreateOrderDto.convertDTO(orderInfo, itemList));
        }
        return null;
    }

    @Override
    public Result<Boolean> cancel(String orderId) {
        OrderInfo orderInfo = orderRepository.queryOrderInfoByOrderId(orderId);
        orderInfo.setYn(0);
        orderInfo.setOrderStatus(OrderStatusType.Cancel.getDbType());
        orderInfo.setUpdateTime(LocalDateTime.now());
        orderRepository.saveOrderInfo(orderInfo);

        return ResultGenerator.success(true);
    }


}
